package sg.edu.nus.iss.team8s.vmcs.PropertyLoader;
/**
 * 
 * @author Nie XiaoYi
 *
 */
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;


import sg.edu.nus.iss.team8s.vmcs.beans.StoreItem;

public class FilePropertyLoader implements PropertyLoader {
	private static String sWorkPath="";
	private static String sWorkFolder = "/data/";
	private String fileName;
	private Properties prop;
	
	private final String NUMOFITEMSKEY = "NumOfItems";
	protected final String NAMEKEY = "Name";
	protected final String QUANTITYKEY = "Quantity";
	
	public FilePropertyLoader(String fileName){
		this.fileName = fileName;
		if(prop == null)
			prop = new Properties();
	}
	
	public StoreItem getItem(int index) {
		// TODO Auto-generated method stub
		return null;
	}

	
	public int getNumOfItems() {
		// TODO Auto-generated method stub
		
		String count = getValue(NUMOFITEMSKEY);
		if(count != null)
			return Integer.parseInt(count);
			
		return 0;
	}

	
	public void initialize() {
		// TODO Auto-generated method stub
		
		String appPath = getClass().getResource(sWorkFolder).getPath();
		try {
			sWorkPath = java.net.URLDecoder.decode(appPath, "UTF-8") ;
		
	
		if (sWorkPath.startsWith("/")){
		sWorkPath=sWorkPath.substring(1);}
		
		File file = new File(sWorkPath + fileName);
		//FileReader reader;
		//reader = new FileReader(file);
		FileInputStream reader = new FileInputStream(file);
		prop.load(reader);
		
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	
	public void saveProperty() {
		// TODO Auto-generated method stub
		String appPath = getClass().getResource(sWorkFolder).getPath();
		try {
			sWorkPath = java.net.URLDecoder.decode(appPath, "UTF-8") ;		
	
		if (sWorkPath.startsWith("/")){
		sWorkPath=sWorkPath.substring(1);}
		
		File file = new File(sWorkPath + fileName);
		//FileWriter writer = new FileWriter(file);
		FileOutputStream writer = new FileOutputStream(file);
		prop.store(writer, "");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
	public void setItem(int index, StoreItem item) {
		// TODO Auto-generated method stub

	}

	
	public void setNumOfItems(int items) {
		// TODO Auto-generated method stub
		setValue(NUMOFITEMSKEY,String.valueOf(items));
	}

	public void setValue(String key,String value){
		if(prop.containsKey(key))
			prop.setProperty(key, value);
	}
	
	public String getValue(String key){	
		if(prop.containsKey(key))
			return prop.getProperty(key);
		return null;
	}
}
